package com.xin.data.simbapesa.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xin.data.simbapesa.R;
import com.xin.data.simbapesa.api.PU;
import com.xin.data.simbapesa.base.BaseDialog;
import com.xin.data.simbapesa.databinding.SimbaFaileDialogBinding;
import com.xin.data.simbapesa.utils.ResUtils;
import com.xin.data.simbapesa.utils.ViewUtils;


public class FailedDialog extends BaseDialog<SimbaFaileDialogBinding> {


    int faiedTime;

    Activity activity;

    public FailedDialog(@NonNull Context context) {
        super(context);
    }

    public FailedDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected FailedDialog(@NonNull Context context, boolean cancelable, @Nullable DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected SimbaFaileDialogBinding initViewBinding(LayoutInflater inflater) {
        return SimbaFaileDialogBinding.inflate(inflater);
    }

    @Override
    protected void initView() {
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        binding.tvBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding != null && activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
                    dismiss();
                    ViewUtils.viewScale(v);
                    if (faiedTime > 3) {
                        faiedTime = 0;
                    }
                    PU.userBuriedPoint(PU.P216, "1$$" + activity.getLocalClassName());
                    if (onBtnClickListener != null) {
                        onBtnClickListener.btnOnClickOk();
                    }
                }
            }
        });
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding != null && activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
                    dismiss();
                    if (faiedTime > 3) {
                        faiedTime = 0;
                    }
                    PU.userBuriedPoint(PU.P216, "2$$" + activity.getLocalClassName());
                    if (onBtnClickListener != null) {
                        onBtnClickListener.btnOnClickOk();
                    }
                }
            }
        });

        setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (binding != null && activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
                    PU.userBuriedPoint(PU.P215, activity.getLocalClassName());
                }
            }
        });

    }

    @Override
    public void show() {
        super.show();
        if (binding != null && activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
            PU.userBuriedPoint(PU.P214, activity.getLocalClassName());
            faiedTime = faiedTime + 1;
            if (faiedTime > 3) {
                binding.tvTitle.setText(ResUtils.s(R.string.faile_hit3));
                binding.tvSubTitle.setVisibility(View.GONE);
            } else {
                binding.tvTitle.setText(ResUtils.s(R.string.faile_hit1));
                binding.tvSubTitle.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }


}
